package com.example.handlerstudy;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private TextView statusTextView = null;
    //uiHandler在主线程中创建，所以自动绑定主线程
    private Handler uiHandler = new Handler(){
      @Override
        public void handleMessage(Message msg){
          if(msg.what==1){
              System.out.println("handleMessage thread id "+ Thread.currentThread().getId());
              //System.out.println("msg.arg1: "+msg.arg1);
              //System.out.println("msg.arg2: "+msg.arg2);
              MainActivity.this.statusTextView.setText("文件下载完成");
          }
      }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        statusTextView = (TextView)findViewById(R.id.statusTextView);
        Button btnDownload = (Button)findViewById(R.id.btnDownload);
        btnDownload.setOnClickListener(this);
        System.out.println("Main thread id "+Thread.currentThread().getId());
    }

    @Override
    public void onClick(View view) {
        DownloadThread downloadThread = new DownloadThread();
        if(view.getId()==R.id.btnDownload) {
            downloadThread.start();
        }


    }

    class DownloadThread extends Thread{
        @Override
        public void run() {
            try{
                System.out.println("DownloadThread id " + Thread.currentThread().getId());
                System.out.println("开始下载文件...");
                //此处让线程DownloadThread休眠5秒钟，模拟文件的耗时过程
                Thread.sleep(5000);
                System.out.println("文件下载完成");
                //文件下载完成后更新UI
                Message msg = new Message();

                //what是我们自定义的一个Message的识别码，以便于在Handler的handleMessage方法中根据what识别
                //出不同的Message,以便于我哦们做出不同得到处理操作
                msg.what =1;

                //我们可以通过arg1和arg2给Message传入简单的数据
                msg.arg1 = 123;
                msg.arg2 = 321;
                //我们也可以通过给obj赋值Object类型传递向Message传入任意数据
                //msg.obj = null;


                //将该Message发送给对应的Handler
                uiHandler.sendMessage(msg);

// ====================================================================================================
//                Runnable runnable = new Runnable() {
//                    @Override
//                    public void run() {
//                        System.out.println("Runnable thread id "+ Thread.currentThread().getId());
//
//                        MainActivity.this.statusTextView.setText("文件下载完成");
//                    }
//                };
//                //将UI更新传递回主线程
//              uiHandler.post(runnable);
//=======================================================================================================
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}
